<template>
  <div>
    <el-form
      :model="updata"
      :rules="rules"
      ref="updata"
      label-width="150px"
      class="demo-ruleForm"
      style="background: #fff; padding: 20px; box-sizing: border-box"
    >
      <el-form-item label="博客名称" prop="blogName">
        <el-input v-model="updata.blogName"></el-input>
      </el-form-item>
      <el-form-item label="作者名" prop="author">
        <el-input v-model="updata.author"></el-input>
      </el-form-item>
      <el-form-item label="作者头像" prop="avatar">
        <template>
          <el-upload
            class="avatar-uploader"
            action=""
            :show-file-list="false"

            :before-upload="beforeAvatarUpload"
            :http-request="setimg"
          >
            <img v-if="updata.avatar" :src="updata.avatar" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </template>
      </el-form-item>

      <el-form-item label="介绍语">
        <el-input type="textarea" v-model="updata.introduction"></el-input>
      </el-form-item>
      <el-form-item label="开启 GitHub 访问" style="text-align: left">
        <el-switch v-model="value" active-color="#13ce66"> </el-switch>
      </el-form-item>
      <el-form-item
        label="GitHub 主页访问地址"
        v-if="value"
        style="text-align: left"
      >
        <el-input
          v-model="updata.githubHome"
          placeholder="请输入 GitHub 主页访问的URL"
        ></el-input>
      </el-form-item>
      <el-form-item label="开启 CSDN 访问" style="text-align: left">
        <el-switch v-model="value1" active-color="#13ce66"> </el-switch>
      </el-form-item>
      <el-form-item
        label="CSDN 主页访问地址"
        v-if="value1"
        style="text-align: left"
      >
        <el-input
          v-model="updata.csdnHome"
          placeholder="请输入 CSDN 主页访问的URL"
        ></el-input>
      </el-form-item>
      <el-form-item label="开启 Gitee 访问" style="text-align: left">
        <el-switch v-model="value2" active-color="#13ce66"> </el-switch>
      </el-form-item>
      <el-form-item
        label="Gitee 主页访问地址"
        v-if="value2"
        style="text-align: left"
      >
        <el-input
          v-model="updata.giteeHome"
          placeholder="请输入 Gitee 主页访问的URL"
        ></el-input>
      </el-form-item>

      <el-form-item label="开启 知乎 访问" style="text-align: left">
        <el-switch v-model="value3" active-color="#13ce66"> </el-switch>
      </el-form-item>
      <el-form-item
        label="知乎 主页访问地址"
        v-if="value3"
        style="text-align: left"
      >
        <el-input
          v-model="updata.zhihuHome"
          placeholder="请输入 知乎 主页访问的URL"
        ></el-input>
      </el-form-item>
      <el-form-item style="text-align: left">
        <el-button type="primary" @click="submitForm()">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { blog, getblog,Set } from "../../../../units/htapi";

export default {
  data() {
    return {
      updata: {
        author: "",
        avatar: "",
        blogName: "",
        csdnHome: "",
        giteeHome: "",
        githubHome: "",
        introduction: "",
        zhihuHome: "",
      },
      value: false,
      value1: false,
      value2: false,
      value3: false,

      rules: {
        blogName: [
          { required: true, message: "请输入博客名称", trigger: "blur" },
          { min: 3, message: "长度大于三个字符", trigger: "blur" },
        ],
        author: [
          { required: true, message: "请输入作者名称", trigger: "blur" },
          { min: 3, message: "长度大于三个字符", trigger: "blur" },
        ],
        avatar: [{ required: true, message: "请上传头像", trigger: "blur" }],
      },
    };

    //
  },
  created() {
    this.getdata();
  },
  methods: {
    async setdata() {
      try {
        let res = await blog(this.updata);
        console.log(res);
      } catch (error) {
        console.log(error, "错误");
      }
    },
    async getdata() {
      try {
        let res = await getblog();
        // console.log(res);
        this.updata = res.data.data;
        this.$message.success('获取用户数据成功')

      } catch (error) {
        this.$message.error('拉取数据失败')
        console.log(error, "错误");
      }
    },
    submitForm() {
      this.$refs["updata"].validate(async(valid) => {
        if (valid) {
          await Set(this.updata)
          this.$message.success("保存成功");
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    handleAvatarSuccess(res, file) {
      this.updata.avatar = URL.createObjectURL(file.raw);
    },
    beforeAvatarUpload(file) {
      const isLt2M = file.size / 1024 / 1024 < 5;

      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isLt2M;
    },
     async setimg(a){
      console.log(a,'aaaa');
      const form = new FormData()
      form.append('file',a.file)
      let res =  await this.$axios.post('/api/admin/file/upload',form)
      this.updata.avatar  =  res.data.data.url
      console.log(this.updata.avatar,'rereres');
      
    }

    //.......................................................................
  },
  //
};
</script>

<style scoped lang="scss">
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 100%;
  height: auto;
  display: block;
}

</style>